package com.ibeetl.cms.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.cms.entity.XxdActivitie;
import com.ibeetl.cms.entity.XxdItem;
import com.ibeetl.cms.service.XxdActivitieService;
import com.ibeetl.cms.service.XxdItemService;
import com.ibeetl.cms.web.query.XxdActivitieQuery;
import com.ibeetl.cms.web.query.XxdItemQuery;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * 最佳销售
 */
@Controller
public class XxdBestController {

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/cms/xxdBest";

    @Autowired
    private XxdItemService xxdItemService;

    @Autowired
    private XxdActivitieService xxdActivitieService;

    @Autowired
    FileService fileService;
    /* 页面 */

    @GetMapping(MODEL + "/index.do")
    @Function("cms.xxdBest.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/cms/xxdBest/index.html") ;
        view.addObject("search", XxdItemQuery.class.getName());
        return view;
    }

    @PostMapping(MODEL + "/list.json")
    @Function("cms.xxdBest.query")
    @ResponseBody
    public JsonResult<PageQuery> list(XxdActivitieQuery condtion)
    {
        PageQuery page = condtion.getPageQuery();
        xxdActivitieService.queryBestByCondition(page);
        return JsonResult.success(page);
    }

    @GetMapping(MODEL + "/details.do")
    @Function("cms.xxdItem.details")
    @ResponseBody
    public ModelAndView details(Long id) {
        ModelAndView view = new ModelAndView("/cms/xxdItem/details.html");
        XxdItem xxdBest = xxdItemService.queryById(id);
        view.addObject("item", xxdBest);
        return view;
    }

    /**
     * 手动更新数据
     * 排名前四名最佳销售
     */
    @PostMapping(MODEL + "/update.do")
    @ResponseBody
    public String update() {
        List<XxdItem> list = xxdActivitieService.queryBestItems();
        xxdActivitieService.removeBest();
        if(list != null){
            for(int i = 0;i < list.size();i++){
                 if(i >= 4){
                    break;
                 }
                XxdItem item = list.get(i);
                XxdActivitie xxdActivitie = new XxdActivitie();
                xxdActivitie.setName("最佳销售");
                xxdActivitie.setItemId(item.getId().intValue());
                xxdActivitie.setActivitieId(3);
                xxdActivitie.setUpdateTime(new Date());
                xxdActivitie.setDel("0");
                xxdActivitie.setStatus("0");
                xxdActivitieService.save(xxdActivitie);
            }
        }else{
            return "500";
        }
        return "200";
    }

}
